from django.core.paginator import Paginator
from django.http import JsonResponse
import datetime

from django_redis import get_redis_connection


def my_decorator(func):
    '''自定义的装饰器:判断是否登录'''

    def wrapper(request, *args, **kwargs):
        if request.user.is_authenticated:
            # 如果用户登录, 则进入这里,正常执行
            return func(request, *args, **kwargs)
        else:
            # 如果用户未登录,则进入这里,返回400的状态码
            return JsonResponse({'errno': '4101',
                                 'errmsg': '请登录后重试'})

    return wrapper


class LoginRequiredMixin(object):
    '''自定义的Mixin扩展类'''

    # 重写的 as_view 方法
    @classmethod
    def as_view(cls, **initkwargs):
        view = super().as_view(**initkwargs)
        # 调用上面的装饰器进行过滤处理:
        return my_decorator(view)


def housecount(house, start_date, end_date):
    redis_conn = get_redis_connection('order')
    date = start_date
    maxnum = 0
    while date < end_date:
        num = redis_conn.hget('order_%s' % house.id, str(date))
        if not num:
            num = 0
        else:
            num = int(num.decode())
        maxnum = max(maxnum, num)
        date += datetime.timedelta(days=1)
    return maxnum





